#include <head.h>

#define BUFFER_SIZE 1028 

typedef struct stu
{
    int fd_1;
    int fd_2;
    int offset;
}stu;
  
void * copy_file(void *s1) 
{  
    stu s2 = *(stu *)s1;
    lseek(s2.fd_1, s2.offset, SEEK_SET);
    char buffer[BUFFER_SIZE];  
    ssize_t num_read;  
    while ((num_read = read(s2.fd_1, buffer, BUFFER_SIZE)) > 0) 
    {  
        write(s2.fd_2, buffer, num_read);  
    }  
  
    close(s2.fd_1);  
    close(s2.fd_2);  
}  
  
int main(int argc, char *argv[]) 
{  
    stu s1;
    if (argc != 3) 
    {  
        fprintf(stderr, "Usage: %s file_1 file_2\n", argv[0]);  
        return 1;  
    }  
  
    int fd_1 = open(argv[1], O_RDONLY);  
    if (fd_1 == -1) 
    {  
        perror("Failed to open file_1");  
        return 1;  
    }  
    s1.fd_1 = fd_1;
    int fd_2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);  
    if (fd_2 == -1) 
    {  
        perror("Failed to open file_2");  
        close(fd_1);  
        return 1;  
    }  
    s1.fd_2 = fd_2;
    int thread = 2; 
    pthread_t tid[thread];
   // 创建线程
    for (int i = 0; i < 2; i++)
    {
        s1.offset = i * BUFFER_SIZE;
        if( 0 != pthread_create( &tid[i] , NULL , copy_file , (void * )&s1) )
        {
            printf("pthread_create error\n");
            return 0;
        }
        // 设定分离态
        pthread_detach(tid[i]);
        while (1)
        {
            /* code */
        }
        
    }
    
    close(fd_1);  
    close(fd_2);  
  
    return 0;  
}